/*
 *	App.cpp is part of Flight_Chess_Portable
 *	Copyright (C) 2008  Poison
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *	Class:			App
 *	Description:	
 *	Author:			Poison <hbpoison@gmail.com>
 *	Date Created:	2008-03-18
 */

#include "App.h"

#include <unistd.h>
#include <cstring>

App::App()
{
	app_path[0] = 0;
	thread_count = 0;
	threads = NULL;
	getPath();
}

App::~App()
{
	exit();
}

int App::start( int argc, void * argv )
{
	if ( init( argc, argv ) < 0 )
	{
		return -1;
	}
	for ( int i = 0; i < thread_count; i ++ )
	{
		threads[i]->start( sizeof( App ), this );
	}
	return 0;
}

int App::exit()
{
	for ( int i = 0; i < thread_count; i ++ )
	{
		delete threads[i];
	}
	thread_count = 0;
	delete threads;
	threads = NULL;
	return 0;
}

const char * App::getPath()
{
	if( app_path[0] == 0 )
	{
		getcwd( app_path, 256 );
		if( app_path[strlen( app_path ) - 1] != '/' )
			strcat( app_path, "/" );
	}
	return app_path;
}

char * App::chPath( const char * path, char  * dest, int size )
{
	if ( chdir( path ) < 0 )
	{
		return NULL;
	}
	getcwd( dest, size );
	if( dest[strlen( dest ) - 1] != '/' )
		strcat( dest, "/" );
	return dest;
}
